package com.subang.function.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.subang.common.core.domain.entity.SysUser;
import com.subang.common.utils.SecurityUtils;
import com.subang.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.subang.common.annotation.Log;
import com.subang.common.core.controller.BaseController;
import com.subang.common.core.domain.AjaxResult;
import com.subang.common.enums.BusinessType;
import com.subang.function.domain.SbUserStudyGroup;
import com.subang.function.service.ISbUserStudyGroupService;
import com.subang.common.utils.poi.ExcelUtil;
import com.subang.common.core.page.TableDataInfo;

/**
 * 用户 - 小组关联Controller
 * 
 * @author subei
 * @date 2025-03-29
 */
@RestController
@RequestMapping("/function/usergroup")
public class SbUserStudyGroupController extends BaseController
{
    @Autowired
    private ISbUserStudyGroupService sbUserStudyGroupService;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询用户 - 小组关联列表
     */

    @GetMapping("/list")
    public TableDataInfo list(SbUserStudyGroup sbUserStudyGroup)
    {
        startPage();
        List<SbUserStudyGroup> list = sbUserStudyGroupService.selectSbUserStudyGroupList(sbUserStudyGroup);
        for(SbUserStudyGroup sbUserStudyGroup1 : list){
            sbUserStudyGroup1.setNickName(userService.selectUserById(sbUserStudyGroup1.getUserId()).getNickName());
        }
        return getDataTable(list);
    }

    /**
     * 导出用户 - 小组关联列表
     */
    @PreAuthorize("@ss.hasPermi('function:group:export')")
    @Log(title = "用户 - 小组关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SbUserStudyGroup sbUserStudyGroup)
    {
        List<SbUserStudyGroup> list = sbUserStudyGroupService.selectSbUserStudyGroupList(sbUserStudyGroup);
        ExcelUtil<SbUserStudyGroup> util = new ExcelUtil<SbUserStudyGroup>(SbUserStudyGroup.class);
        util.exportExcel(response, list, "用户 - 小组关联数据");
    }

    /**
     * 获取用户 - 小组关联详细信息
     */
    @PreAuthorize("@ss.hasPermi('function:group:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sbUserStudyGroupService.selectSbUserStudyGroupById(id));
    }

    /**
     * 新增用户 - 小组关联
     */
    @Log(title = "用户 - 小组关联", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SbUserStudyGroup sbUserStudyGroup)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        sbUserStudyGroup.setUserId(user.getUserId());
        List<SbUserStudyGroup> list = sbUserStudyGroupService.selectSbUserStudyGroupList(sbUserStudyGroup);
        if(list.size()>0){
            return AjaxResult.error("你已经加入该学习小组");
        }else{
            return toAjax(sbUserStudyGroupService.insertSbUserStudyGroup(sbUserStudyGroup));
        }
    }

    /**
     * 修改用户 - 小组关联
     */
    @PreAuthorize("@ss.hasPermi('function:group:edit')")
    @Log(title = "用户 - 小组关联", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SbUserStudyGroup sbUserStudyGroup)
    {
        return toAjax(sbUserStudyGroupService.updateSbUserStudyGroup(sbUserStudyGroup));
    }

    /**
     * 删除用户 - 小组关联
     */
    @Log(title = "用户 - 小组关联", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sbUserStudyGroupService.deleteSbUserStudyGroupByIds(ids));
    }

    @PostMapping("/{groupId}")
    public AjaxResult removeByGroupId(@PathVariable String groupId)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        return toAjax(sbUserStudyGroupService.deleteSbUserStudyGroupByUserIdAndGroupId(user.getUserId(),groupId));
    }

    @PostMapping("/delete")
    public AjaxResult groupMemberDelete(@RequestBody SbUserStudyGroup sbUserStudyGroup)
    {

        return toAjax(sbUserStudyGroupService.deleteSbUserStudyGroupByUserIdAndGroupId(sbUserStudyGroup.getUserId(),sbUserStudyGroup.getGroupId()));
    }
}
